package com.silverbox.core.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;

/**
 * 时间工具类
 * @author YaoXuYang
 * @date 2017年12月18日
 * @description
 */
public class DateUtils {

    public final static String YYYYMMDD = "yyyy-MM-dd";
    public final static String YYYYMMDD_REGEX = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))?$";
    public final static String HHMM= "HH:mm";
    public final static String HHMM_REGEX = "^([01]\\d|2[01234]):([0-5]\\d|60)$";
    public final static String YYYYMMDD_HHMM= "yyyy-MM-dd HH:mm";
    public final static String YYYYMMDD_HHMM_REGEX = "^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))\\s+([0-1]?[0-9]|2[0-3]):([0-5][0-9])$";

    /**
	 * 当前时间
	 * @return Date
	 */
	public static Date currentTime(){
		Calendar calendar = Calendar.getInstance();   
		calendar.setTime(new Date()); 
		return calendar.getTime(); 
	}
	
	/**
	 * 获取当前日期
	 * 
	 * @return String
	 */
	public static String getNowTime() {
		String result = "";
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		Date date = calendar.getTime();
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		result = format.format(date);
		return result;
	}
	
	/**
	 * 15天前时间
	 * @return Date
	 */
	public static Date fifteenDaysAgoTime(){
		Calendar calendar = Calendar.getInstance();   
		calendar.setTime(new Date());
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) - 15); // 7天之前的时间
		calendar.set(Calendar.DATE, calendar.get(Calendar.DATE));
		return calendar.getTime();
	}
	
	/**
	 * 七天前时间
	 * @return Date
	 */
	public static Date sevenDaysAgoTime(){
		Calendar calendar = Calendar.getInstance();   
		calendar.setTime(new Date());
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) - 7); // 7天之前的时间
		calendar.set(Calendar.DATE, calendar.get(Calendar.DATE));
		return calendar.getTime();
	}
	
	/**
	 * 获取今天开始时间
	 * @return Date
	 */
    public static String getTodayStartTime() {
		String result = "";
		Calendar c = Calendar.getInstance();
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE,0);
		c.set(Calendar.SECOND, 0);
		Date date = c.getTime();
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		result = format.format(date);
		return result;
	}
	/**
	 * 获取今天结束时间
	 * @return Date
	 */
    public static String getTodayEndTime() {
		String result = "";
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, 1);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE,0);
		c.set(Calendar.SECOND, 0);
		Date date = c.getTime();
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		result = format.format(date);
		return result;
	}

	/**
	 * 获取昨天开始时间
	 * 
	 * @author litao
	 * @date 2017年12月27日
	 */
	public static String getYesterdayStartTime() {
		String result = "";
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, -1);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		Date date = c.getTime();
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		result = format.format(date);
		return result;
	}

	/**
	 * 获取当天星期几
	 * 7是星期日、1是星期一、2是星期二、3是星期三、4是星期四、5是星期五、6是星期六
	 * @return int  
	 * @author litao
	 * @date 2018年1月18日
	 */
	public static int getDayofweek(){  
		int[] dateWeek = { 7, 1, 2, 3, 4, 5, 6 };
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		if (week < 0) {
			week = 0;
		}
		return dateWeek[week];
	}
	
	/**
	 * Date类型装换成String
	 * 
	 * @param  date
	 * @return String  
	 * @author litao
	 * @date 2018年5月17日
	 */
	public static String getDateChangeString(Date date) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String timeStr = null;
		if (date != null) {
			timeStr = formatter.format(date);
		}
		return timeStr;
	}
	
	/**
	 * 分钟转化成当前日期
	 * @param clearCartTime
	 * @return
	 */
	public static String transTimeForString(Integer clearCartTime){
		long nowTime = new Date().getTime();
		long clearTime = clearCartTime * 60 * 1000;
		long time = nowTime - clearTime;
		Date date = new Date(time);
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sd.format(date);
	}
	
	/**
	 * 分钟转化成当前日期
	 * @param clearCartTime
	 * @return
	 */
	public static Date transTimeForDate(Integer clearCartTime){
		long nowTime = new Date().getTime();
		long clearTime = clearCartTime * 60 * 1000;
		long time = nowTime - clearTime;
		Date date = new Date(time);
		return date;
	}
	
	/**
	 * 获取当前时间之前的几小时时间 
	 * @param count
	 * @return
	 */
	public static String getBeforeDate(int count){ 
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    Calendar calendar = Calendar.getInstance();
	    Date date=new Date();
	    calendar.setTime(date);
	    calendar.add(Calendar.HOUR, -count);
	    date = calendar.getTime();
	    return sdf.format(date);
	}
	

	  /**
	   * 根据当前时间获取index个月前的日期
	   * @param date
	   * @param index
	   * @return
	   */
	  public static Date getDateForXBeaforeMonth(Date date, Integer index){
		  Calendar calendar = Calendar.getInstance(); //得到日历
		  calendar.setTime(date);//把当前时间赋给日历
		  calendar.add(calendar.MONTH, -index); //设置为前2月
		  return calendar.getTime();
	  }

    /**
     * 根据当前时间获取index个月后的日期
     * @param date
     * @param index
     * @return
     */
    public static Date getDateForXAfterMonth(Date date, Integer index){
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(date);//把当前时间赋给日历
        calendar.add(calendar.MONTH, +index); //设置为后2月
        return calendar.getTime();
    }
	  
	  /**
	   * 时间格式化
	   * @param date
	   * @return
	   */
	  public static Date dateFormat(String date){
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return sdf.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	  }
	  
	 /**
     * 判断当前时间是否在时间范围内
     * @param startTime
     * @param endTime
     * @return
     */
    public static boolean isNowBetweenTimes (String startTime, String endTime) {
    	// 其中一个时间为空时，即不在时间范围内
    	if (Utils.isEmpty(startTime) || Utils.isEmpty(endTime)) {
    		return false;
    	}
    	// 两个时间相等，即全天营业/全天打烊
    	if (startTime.equals(endTime)) {
    		return true;
    	}
    	if ("00:00".equals(startTime) && "23:59".equals(endTime)) {
    		return true;
    	}
    	int nowHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
		int nowMinute = Calendar.getInstance().get(Calendar.MINUTE);
		int nowSecond = Calendar.getInstance().get(Calendar.SECOND);
		String nowTimeStr = nowHour + ":" + nowMinute + ":" + nowSecond;
		String format = "HH:mm:ss";
		try {
			Date nowTime = new SimpleDateFormat(format).parse(nowTimeStr);
			Date start = new SimpleDateFormat(format).parse(startTime + ":00");
			Date end = new SimpleDateFormat(format).parse(endTime + ":59");
			// 先判断营业时间/打烊时间是否跨天
			int type = 1;
			if (start.after(end)) {
				type = 2;
			}
			// 如果不跨天
			if (type == 1) {
				if (nowTime.after(start) && nowTime.before(end)) {
					return true;
		        } else {
		        	return false;
		        }
			} else {
				if (nowTime.after(start) || (nowTime.before(end))) {
					return true;
				} else {
					return false;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
    }
    
	/**
	 * 获取当天星期几
	 * 7是星期日、1是星期一、2是星期二、3是星期三、4是星期四、5是星期五、6是星期六
	 * @return int  
	 * @author litao
	 * @date 2018年1月18日
	 */
	public static int getDayofweek(Date date){  
		int[] dateWeek = { 7, 1, 2, 3, 4, 5, 6 };
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		if (week < 0) {
			week = 0;
		}
		return dateWeek[week];
	}

	/*
	 *<pre>
	 * 说    明: 正则校验日期格式
	 * 涉及版本:
	 * 创 建 者: YangYangjun
	 * 日    期: 2020-05-25 11:23
	 *</pre>
	 **/
	public static boolean matchDateStyle(String dateString,String dateStyle){
	    boolean b = false;
	    switch (dateStyle){
            case YYYYMMDD:
                return Pattern.compile(YYYYMMDD_REGEX).matcher(dateString).matches();
            case HHMM:
                return Pattern.compile(HHMM_REGEX).matcher(dateString).matches();
            case YYYYMMDD_HHMM:
                return Pattern.compile(YYYYMMDD_HHMM_REGEX).matcher(dateString).matches();
        }
	    return b;
    }
}
